β‘ How a GitHub Actions Runner Works (Step by Step)
GitHub Actions runners execute workflows when triggered by events like code pushes, pull requests, or scheduled tasks. Hereβs a step-by-step breakdown of how it works:
π 1. Triggering the Pipelineβ
- You push or pull code to GitHub (or trigger any defined event). π
- GitHub detects this event and assigns the job to an available runner (either GitHub-hosted or self-hosted). π₯οΈ
π Example Triggers:
push
tomain
branch π€- Opening a
pull request
π - A
cron job
running at scheduled times β°
π₯ 2. Runner Fetches the Codeβ
- The runner starts on a machine (either a GitHub-provided VM or your own self-hosted server). ποΈ
- It clones (downloads) the latest code from your GitHub repository. π
- The runner sets up the working environment as defined in the YAML workflow file.
π οΈ 3. Runner Executes the Stepsβ
- It follows the instructions defined in the GitHub Actions YAML file (
.github/workflows/your-workflow.yml
). - Each step is executed in the specified order.
π Example Steps:
β
Set up Node.js π§
β
Install dependencies (e.g., npm install
) π¦
β
Run tests (e.g., npm test
) π§ͺ
β
Build the project (e.g., npm run build
) ποΈ
β
Lint and format code (e.g., eslint . --fix
) π¨
π‘ 4. Deploying to Another Server (Optional Step)β
- The runner itself does not run your application; it only executes tasks like testing, building, and deploying.
- If your YAML file includes an SSH step, the runner will:
- π Use SSH to connect to the production server.
- π€ Copy the built files to the production server using
rsync
orscp
. - π Restart services (e.g.,
pm2 restart app
,docker-compose up -d
).
π― Key Takeawaysβ
β
GitHub Actions automates testing, building, and deployment processes.
β
Runners execute jobs based on YAML workflow instructions.
β
Self-hosted runners provide more control, while GitHub-hosted runners are managed automatically.
β
Deployments often involve SSH & file transfers to production environments.
π‘ With GitHub Actions, you can automate your entire CI/CD pipeline effortlessly! ππ₯
π GitHub Actions CI/CD Pipeline with Deployment
π οΈ Prerequisites:β
Before running the pipeline, you must store your serverβs private key securely in GitHub:
- Go to your GitHub Repository β
Settings
βActions
βSecrets and Variables
. - Add a new secret with the name
DOCUMENT_UBUNTU_SERVER
.
π GitHub Actions Workflow YAMLβ
name: GitHub Action push-based CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest ## Replace with self-hosted runner if needed
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Use Node.js 22
uses: actions/setup-node@v3
with:
node-version: 22
- name: Install dependencies
run: npm install -f
- name: Build project
run: npm run build
- name: Setup SSH Key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.DOCUMENT_UBUNTU_SERVER }}" | tr -d '\r' > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan -H 13.201.191.75 >> ~/.ssh/known_hosts
- name: Deploy build files to EC2
run: |
rsync -avz --delete -e "ssh -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no" ./build/ root@13.201.191.75:/var/www/html/my-docs/build/
π Explanation of Each Step:β
- Checkout Code: Pulls the latest code from GitHub.
- Set Up Node.js: Uses version 22 for the project.
- Install Dependencies: Ensures all required packages are installed.
- Build Project: Runs the build command.
- Setup SSH Key:
- Stores the private key securely.
- Adds the remote server to
known_hosts
to prevent SSH confirmation prompts.
- Deploy to EC2:
- Uses
rsync
to copy the build files securely to the production server.
- Uses
π Final Thoughtsβ
β
Automates deployment with every push to main
.
β
Secures SSH access with GitHub Secrets .
β
Ensures seamless deployments by syncing only necessary files.
π‘ With this setup, you achieve a fully automated, secure, and efficient CI/CD pipeline! π₯π₯